package com.hxz.lesson04;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLDown {
    public static void main(String[] args) throws Exception{
        //1、下载地址
        URL url = new URL("https://m801.music.126.net/20201224165158/8c012609663581ab53c7e08e0630bdba/jdyyaac/035f/525e/005c/458b6759a9e4ff826e6876b238cffcb2.m4a");

        //2、连接到这个资源 HTTP
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();

        FileOutputStream fos = new FileOutputStream("shaonianjinshi.m4a");

        byte[] buffer = new byte[1024];
        int len;
        while ((len=inputStream.read(buffer))!=-1) {
            fos.write(buffer, 0, len);
        }
        fos.close();
        inputStream.close();
        urlConnection.disconnect();

    }
}
